/**
 * Copyright (C), 2015-2021, XXX有限公司
 * FileName: PersonalHomeActivity
 * Author: Gxjkz
 * Date: 2021/7/14 10:48
 * Description: 个人主页
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
package com.niuxiaofa.newlaw.Activity;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;

import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.tabs.TabLayout;
import com.melnykov.fab.FloatingActionButton;
import com.niuxiaofa.newlaw.Base.BaseActivity;
import com.niuxiaofa.newlaw.Fragment.DynamicFragment;
import com.niuxiaofa.newlaw.Fragment.VideoFragment;
import com.niuxiaofa.newlaw.R;
import com.niuxiaofa.newlaw.Tools.ToActivityUtil;
import com.niuxiaofa.newlaw.Tools.UIUtils;
import com.niuxiaofa.newlaw.adapter.CustomViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @ClassName: PersonalHomeActivity
 * @Description: java类作用描述
 * @Author: Gxjkz
 * @Date: 2021/7/14 10:48
 */
public class PersonalHomeActivity extends BaseActivity {

    private FragmentPagerAdapter fragmentPagerAdapter;

    @BindView(R.id.customViewPager)
    CustomViewPager customViewPager;

    DynamicFragment dynamicFragment = new DynamicFragment();
    VideoFragment videoFragment = new VideoFragment();

    private String[] tabTxt = {"动态", "视频", "收藏"};

    @BindView(R.id.tablayout_holder)
    TabLayout holderTabLayout;

    @BindView(R.id.appBarLayout)
    AppBarLayout appBarLayout;

    @BindView(R.id.toolbar_titletv)
    TextView toolbar_titletv;

    int currentPage = 0;

    @BindView(R.id.follow)
    LinearLayout follow;

    @BindView(R.id.user_bg)
    ImageView user_bg;

    @BindView(R.id.fab)
    FloatingActionButton floatingActionButton;

    private List<Fragment> mFragments = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_personal_home;
    }

    @Override
    protected void initData() {
        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {


                if (verticalOffset == 0) {
                    toolbar_titletv.setText("");
                } else {
                    toolbar_titletv.setText("牛晓法");
                }
            }
        });


        fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }

            @Override
            public int getCount() {
                return mFragments.size();
            }
        };

        mFragments.add(dynamicFragment);
        mFragments.add(videoFragment);

        customViewPager.setOffscreenPageLimit(4);
        customViewPager.setScanScroll(true);
        customViewPager.setAdapter(fragmentPagerAdapter);


        for (int i = 0; i < tabTxt.length; i++) {
            holderTabLayout.addTab(holderTabLayout.newTab().setText(tabTxt[i]));
        }


        //实际的tablayout的点击切换
        holderTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int pos = tab.getPosition();
                customViewPager.setCurrentItem(pos);
                currentPage = pos;
                switch (pos) {
                    case 0:
                        floatingActionButton.setVisibility(View.VISIBLE);
                        floatingActionButton.setImageBitmap(UIUtils.getBitmap(R.mipmap.edit));
                        break;
                    case 1:
                        floatingActionButton.setVisibility(View.VISIBLE);
                        floatingActionButton.setImageBitmap(UIUtils.getBitmap(R.mipmap.add));
                        break;
                    case 2:
                        floatingActionButton.setVisibility(View.GONE);
                        break;

                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        floatingActionButton.setOnClickListener(e -> {
            switch (currentPage) {

                case 0:

                    ToActivityUtil.toNextActivity(PublishDynamicActivity.class);
                    break;

                case 1:
                    String[][] a = new String[][]{{"type", "1"}};
                    ToActivityUtil.toNextActivity(PublishDynamicActivity.class, a);
                    break;

            }
        });

        follow.setOnClickListener(e -> {

            ToActivityUtil.toNextActivity(FocusActivity.class);

        });
    }
}